* Table A9: Intergenerational Mobility in the 1948-1953 and 1961-1964 Cohorts, Multiply Imputing Missing Income
eststo clear
set seed 106
set sortseed 83193

clear
use "${data_dir}DavisMazumderData.dta"

keep if age_firstSurvey<=18
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 
 
* Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  & miss0==0 & miss1==0  [w=weight]
 replace N = r(N) if surv79==0 & women==0  & miss0==0 & miss1==0 
 tab n  if surv79==0 & women==1  & miss0==0 & miss1==0   [w=weight]
 replace N = r(N) if surv79==0 & women==1  & miss0==0 & miss1==0 
 tab n  if surv79==1 & women==0  & miss0==0 & miss1==0   [w=weight]
 replace N = r(N) if surv79==1 & women==0   & miss0==0 & miss1==0 
 tab n  if surv79==1 & women==1  & miss0==0 & miss1==0  [w=weight]
 replace N = r(N) if surv79==1 & women==1  & miss0==0 & miss1==0 
 
* Family Income Rank in Parent Generation
gen faminc0_nm = faminc0
replace faminc0_nm = . if !(miss0==0 & miss1==0)
set sortseed 6688571
sort women surv79 faminc0_nm
by women surv79: gen __rank0 = sum(weight) if  miss0==0 & miss1==0
gen _rank0 = __rank0/N if  miss0==0 & miss1==0
by women surv79 faminc0_nm: egen rank0 = mean(_rank0) if  miss0==0 & miss1==0
 replace rank0 = 100*rank0
gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

 
* Family Income Rank in Kid Generation
gen faminc1_nm = faminc1
 replace faminc1_nm = . if !(miss0==0 & miss1==0)
set sortseed 6688571
sort women surv79 faminc1_nm
by women surv79: gen __rank1 = sum(weight) if  miss0==0 & miss1==0
gen _rank1 = __rank1/N if  miss0==0 & miss1==0
bys women surv79 faminc1_nm: egen rank1 = mean(_rank1) if  miss0==0 & miss1==0
replace rank1 = 100*rank1


drop N __rank0 _rank0 __rank1 _rank1 
 
keep rank1 rank0 lfaminc1 lfaminc0 mom_age_at_birth_b dad_age_at_birth_b ms_kid1 ms_kid2 ms_kid3 ms_parent1 ms_parent2 ms_parent3 weight hhid surv79 women black married hgc 

gen hgc_ms = missing(hgc)
replace hgc = 0 if missing(hgc)

gen married_ms = missing(married)
replace married = 0 if married_ms==1

gen hgc2 = hgc*hgc

gen mom_age_at_birth_b2 = mom_age_at_birth_b*mom_age_at_birth_b
gen dad_age_at_birth_b2 = dad_age_at_birth_b*dad_age_at_birth_b

mi set flong

/* Register Which Variables Will and Will Not Be Imputed */
mi register imputed rank1 rank0 lfaminc1 lfaminc0
mi register regular mom_age_at_birth_b dad_age_at_birth_b ///
	ms_kid1 ms_kid2 ms_kid3 ms_parent1 ms_parent2 ms_parent3 ///
	mom_age_at_birth_b2 dad_age_at_birth_b2 black married hgc hgc2 hgc_ms married_ms



/* Impute  */
 mi impute chained (pmm, knn(10)) rank1 rank0  lfaminc1 lfaminc0 ///
	=  mom_age_at_birth_b  mom_age_at_birth_b2 dad_age_at_birth_b  ///
		dad_age_at_birth_b2 black married hgc hgc2  ///
		ms_kid1 ms_kid2 ms_kid3 ms_parent1 ms_parent2 ms_parent3 hgc_ms married_ms ///
	, by(surv79 women) add(20) rseed(1272886) force
	

	 mi passive: gen perc66  = rank0*(surv79==0)
	 mi passive: gen perc79  = rank0*(surv79==1)	
	 mi passive: gen loginc66  = lfaminc0*(surv79==0)
	 mi passive: gen loginc79  = lfaminc0*(surv79==1)	 

eststo clear	
	
	* --------- Rank-Rank --------- *

* Pooled
 mi estimate, cmdok post esample(s): reg rank1 perc66 perc79 surv79 women [w=weight] , cluster(hhid)
eststo rank_p
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_p
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_p
drop s

 mi estimate (_b[perc79]-_b[perc66]), cmdok post: reg rank1 perc66 perc79 surv79 women [w=weight] , cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: rank_p
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): rank_p
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): rank_p
global rank_main_est = r(estimate)
global rank_main_se = r(se)


* Women
 mi estimate, cmdok post esample(s): reg rank1 perc66 perc79 surv79 if women==1 [w=weight] , cluster(hhid)
eststo rank_w
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_w
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_w
drop s

mi estimate (_b[perc79]-_b[perc66]), cmdok post: reg rank1 perc66 perc79 surv79 women [w=weight] if women==1 , cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: rank_w
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): rank_w
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): rank_w


* Men
 mi estimate, cmdok post esample(s): reg rank1 perc66 perc79 surv79 if women==0 [w=weight] , cluster(hhid)
eststo rank_m

count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_m
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_m
drop s

mi estimate (_b[perc79]-_b[perc66]), cmdok post: reg rank1 perc66 perc79 surv79 women [w=weight] if women==0 , cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: rank_m
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): rank_m
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): rank_m



* --------- IGE --------- *

* Pooled
 mi estimate, cmdok post esample(s): reg lfaminc1 loginc66 loginc79 surv79 women [w=weight], cluster(hhid)
eststo IGE_p

count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_p
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_p
drop s

mi estimate (_b[loginc79]-_b[loginc66]), cmdok post: reg lfaminc1 loginc66 loginc79 surv79 women [w=weight], cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: IGE_p
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): IGE_p
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): IGE_p
global ige_main_est = r(estimate)
global ige_main_se = r(se)

* Women
 mi estimate, cmdok post esample(s): reg lfaminc1 loginc66 loginc79 surv79 if women==1 [w=weight], cluster(hhid) 
eststo IGE_w

count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_w
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_w
drop s

mi estimate (_b[loginc79]-_b[loginc66]), cmdok post: reg lfaminc1 loginc66 loginc79 surv79 women [w=weight] if women==1, cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: IGE_w
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): IGE_w
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): IGE_w

* Men
 mi estimate, cmdok post esample(s): reg lfaminc1 loginc66 loginc79 surv79 if women==0 [w=weight], cluster(hhid)
eststo IGE_m

count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_m
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_m
drop s

mi estimate (_b[loginc79]-_b[loginc66]), cmdok post: reg lfaminc1 loginc66 loginc79 surv79 women [w=weight] if women==0, cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: IGE_m
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): IGE_m
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): IGE_m

#delimit ;
estout rank_p IGE_p rank_w IGE_w rank_m IGE_m
	using  "${results_dir}tableA9.txt", replace
	keep(perc66 perc79) 
	rename(loginc66  perc66 loginc79 perc79)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(diff se p N66 N79) stardrop(*) ;	
#delimit cr	


exit













